#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <unistd.h>

#define COMPILE_INFO_FILE "compile_info.txt"
#define USAGE_TIME_FILE "usage_time.csv"
#define PREPARE_TIME_FILE "prepare_time.csv"

int main(int argc, char *argv[])
{
	const char *usage="usage:\n"
			"\t./a.exe file_name\n";

	unsigned long time;
	double dtime;
	unsigned long end_time;
	FILE *fp_source;
	FILE *fp_compile;
	FILE *fp_usage_time;
	
	char buf[8192];
	char out[2048];
	char *file_name;
	char *action;
	char *hour;
	char *minute;
	char *second;
	char *msec;
	char *p;

	
	if(argc<2)
	{
		printf("%s",usage);
		return -1;
	}

//=======================================================
	fp_source=fopen(argv[1],"r");
	if(NULL==fp_source)
	{
		printf("file %s open fail!\n",argv[1]);
		return -1;
	}

	fp_compile=fopen(COMPILE_INFO_FILE,"w");
	if(NULL==fp_compile)
	{
		printf("file %s open fail!\n",COMPILE_INFO_FILE);
		return -1;
	}

	while(NULL!=fgets(buf,sizeof(buf),fp_source))
	{
		if(NULL!=strstr(buf,"comile")){

			p=strchr(buf,' ');
			p++;
			file_name=p;
			p=strchr(p,' ');
			*(p++)=0;
			
			action=p;//start_time: 9:31:32.43			
			p=strchr(p,':');
			*(p++)=0;

			//hour
			hour=p;// 9:31:32.43
			p=strchr(p,':');
			*(p++)=0;	

			
			minute=p;//31:32.43
			p=strchr(p,':');
			*(p++)=0;	

			second=p;//32.43
			p=strchr(p,'.');
			*(p++)=0;

			msec=p;//43
			*(p+2)=0;
			
#if 1
		
			
			time=3600*atol(hour);
			time+=60*atol(minute);
			time+=atol(second);
			time*=100;
			time+=atol(msec);
			//dtime=((double)time/100);
			
			sprintf(out,"time=%d,action=%s,file_name=%s\n",
				time,action,file_name);	
#endif
#if 0
			sprintf(out,"action=%s,hour=%s,minute=%s,second=%s,msec=%s,file_name=%s\n",
				action,hour,minute,second,msec,file_name);
#endif
			fputs(out,fp_compile);
		}
	}
	
	fclose(fp_compile);
	fclose(fp_source);

//=======================================================

	fp_compile=fopen(COMPILE_INFO_FILE,"r");
	if(NULL==fp_compile)
	{
		printf("file %s open fail!\n",COMPILE_INFO_FILE);
		return -1;
	}
	
	fp_usage_time=fopen(USAGE_TIME_FILE,"w");
	if(NULL==fp_usage_time)
	{
		printf("file %s open fail!\n",USAGE_TIME_FILE);
		return -1;
	}	

	sprintf(out,"time,file_name\n");
	fputs(out,fp_usage_time);

	while(1)
	{
		//time=4805855,action=start_time,file_name=E:/gps/gps.9501/9501_ARM_TREE/MNL/MNL_SRC/XG4K_Nav_Common/User_Config.c
		
		//start time
		if(NULL==fgets(buf,sizeof(buf),fp_compile))
		{
			break;
		}
		
		
		strtok(buf,"=,");
		p=strtok(NULL,"=,");
		time=atol(p);
		//end time
		if(NULL==fgets(buf,sizeof(buf),fp_compile))
		{
			break;
		}
		
		
		strtok(buf,"=,");
		p=strtok(NULL,"=,");
		end_time=atol(p);
		strtok(NULL,"=,");
		strtok(NULL,"=,");
		strtok(NULL,"=,");
		file_name=strtok(NULL,"=,");
		
		if(end_time>time){
			time=end_time-time;
		}else{
			time=24*60*60*100-time+end_time;
		}
		dtime=((double)time/100);
		sprintf(out,"%06.2f,%s",dtime,file_name);
		fputs(out,fp_usage_time);
	}
	fclose(fp_compile);
	fclose(fp_usage_time);

//===========================================================================
	fp_compile=fopen(COMPILE_INFO_FILE,"r");
	if(NULL==fp_compile)
	{
		printf("file %s open fail!\n",COMPILE_INFO_FILE);
		return -1;
	}
	
	fp_usage_time=fopen(PREPARE_TIME_FILE,"w");
	if(NULL==fp_usage_time)
	{
		printf("file %s open fail!\n",PREPARE_TIME_FILE);
		return -1;
	}	

	sprintf(out,"time,file_name\n");
	fputs(out,fp_usage_time);
	fgets(buf,sizeof(buf),fp_compile);
	while(1)
	{
		//time=4805855,action=start_time,file_name=E:/gps/gps.9501/9501_ARM_TREE/MNL/MNL_SRC/XG4K_Nav_Common/User_Config.c
		
		//start time
		if(NULL==fgets(buf,sizeof(buf),fp_compile))
		{
			break;
		}
		
		
		strtok(buf,"=,");
		p=strtok(NULL,"=,");
		time=atol(p);
		//end time
		if(NULL==fgets(buf,sizeof(buf),fp_compile))
		{
			break;
		}
		
		
		strtok(buf,"=,");
		p=strtok(NULL,"=,");
		end_time=atol(p);
		strtok(NULL,"=,");
		strtok(NULL,"=,");
		strtok(NULL,"=,");
		file_name=strtok(NULL,"=,");
		
		if(end_time>time){
			time=end_time-time;
		}else{
			time=24*60*60*100-time+end_time;
		}
		dtime=((double)time/100);
		sprintf(out,"%06.2f,%s",dtime,file_name);
		fputs(out,fp_usage_time);
	}
	fclose(fp_compile);
	fclose(fp_usage_time);


	return 0;
}

